import {
    cacheTools,
} from 'nc-lightapp-front';

import CommonAction from './common';
import {COMMON} from "../main/util";

export default class TableAction extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
    }

    // 获取主表数据
    getMainTableData = async ({
                                  conditions,
                                  stapplyMode,
                                  trnsType,
                                  pageInfo,
                                  pk_org,
                                  beginTime,
                                  billState,
                                  endTime,
                                  billTime,
                                  callback
                              } = {}) => {
        const {props} = this.comp;
        const {transfer, dispatch, editTable} = props;
        // 保证调用方式有两种，传参，如果没有传惨就从store拿
        !conditions && (conditions = props.search.getQueryInfo(COMMON.searchId, true));
        (typeof stapplyMode==='undefined') && (stapplyMode = transfer.stapplyMode);
        (typeof trnsType==='undefined') && (trnsType = transfer.trnsType.refpk);
        !pageInfo && (pageInfo = transfer.pageInfo);
        !pk_org && (pk_org = transfer.hrorgobj ? transfer.hrorgobj.refpk : '');
        !billState && (billState = transfer.orderStatus);
        !beginTime && (beginTime = transfer.beginTime);
        !endTime && (endTime = transfer.endTime);
        let oid = transfer.oid;
        !billTime && (billTime = transfer.orderTimeRange);

        if(!oid) return;
        let postData = {
            "querytype": "tree",
			"querycondition": {
				"logic":"and",
				"conditions":[]
			},
            oid,
            stapplyMode,
            trnsType,
            pageInfo,
            pk_org,
            billState,
            billTime,
            beginTime,
            endTime,
            ...conditions
        };

        try {
            let res = await dispatch({
                type: 'transfer/getMainTableData',
                payload: {
                    postData: postData
                }
            });
            let tableId = COMMON.tableId;
            if (res.success) {
                if (callback && typeof callback === 'function') {
                    callback(res);
                }
                if (res.data && res.data.list) {
                    // 详情页翻页组件用
                    let billId = res.data.list.allpks || [];
                    cacheTools.set('allpks', billId);

                    let pageInfo = this.deepCopy(res.data.list.pageInfo);
                    dispatch({
                        type: 'transfer/update',
                        payload: {
                            pageInfo
                        }
                    });

                    editTable.setTableData(tableId, res.data[tableId], false);
                    editTable.showColByKey(tableId, 'opr')
                } else {
                    cacheTools.set('allpks', []);
                    editTable.setTableData(tableId, {rows: []}, false);
                }
            }
        } catch (e) {

        }
    }

    // 当用户点击了全部选择按钮
    selectAll = (props, tableId, status) => {
        this.comp.props.dispatch({
            type: 'transfer/update',
            payload: {
                tableSelectAll: status
            }
        });
    }

    // 行上的操作按钮点击
    rowClickHandle = (key, record, index) => {
        const {props, action} = this.comp;
        const {transfer, editTable, dispatch} = props;

        let selectedRows = [record];

        console.log(key, record);

        // 删除
        if (key === 'delete') {
            action.rowAct.deleteRows(selectedRows);
        }
        // 编辑
        else if (key === 'edit') {
            action.rowAct.editOnes(selectedRows);
        }
        // 复制
        else if (key === 'copy') {
            action.rowAct.copyOnes(selectedRows);
        }
        // 发送offer
        else if (key === 'send_offer') {
            action.offerAct.openOfferModal(selectedRows);
        }
        // 接受offer
        else if (key === 'accept_offer') {
            action.offerAct.acceptOffer(selectedRows);
        }
        // 拒绝offer
        else if (key === 'reject_offer') {
            action.offerAct.rejectOffer(selectedRows);
        }
        // 提交offer
        else if (key === 'submit_offer') {
            action.offerAct.submitOffer(selectedRows);
        }
        // 附件
        else if (key === 'file_manager') {
            action.mainAct.fileManager(selectedRows);
        }
        // 查看审批详情
        else if (key === 'approveinfo') {
            action.rowAct.previewApproveInfo(selectedRows);
        }
    }

    // 主页面主表格行双击事件
    doubleClick = (record, index, fProps, e) => {
        const {props, action} = this.comp;
        const {transfer, editTable, dispatch} = props;

        action.formAct.toBrowsePage([record]);
    }


    //每页显示条数
    pageSizeSelect = async (val) => {
        const {props: {transfer, transfer: {pageInfo}, dispatch}} = this.comp;
        pageInfo.pageSize = val;
        pageInfo.pageIndex = 1;

        await dispatch({
            type: 'transfer/update',
            payload: {
                pageInfo
            }
        });
        if(transfer.widget){
            this.getWidgetTableData({pageInfo:pageInfo})
        }else{
            this.getMainTableData();
        }
    };

    //分页事件
    paginationEve = async (key) => {
        const {props: {transfer, transfer: {pageInfo}, dispatch}} = this.comp;
        pageInfo.pageIndex = key;

        await dispatch({
            type: 'transfer/update',
            payload: {
                pageInfo
            }
        });
        if(transfer.widget){
            this.getWidgetTableData({pageInfo:pageInfo})
        }else{
            this.getMainTableData();
        }
    };


    getWidgetTableData = async ({
        value,
        pageInfo,
    } = {}) => {
        const {props} = this.comp;
        const {transfer, dispatch, editTable} = props;
        // 保证调用方式有两种，传参，如果没有传惨就从store拿
        !pageInfo && (pageInfo = transfer.pageInfo);
        let postData = {
            widgetCode:"6008C070",
            pageInfo: {
				pageSize: parseInt(pageInfo.pageSize),
				total: parseInt(pageInfo.total),
				totalPage:parseInt(pageInfo.totalPage),
				pageIndex:  parseInt(pageInfo.pageIndex),
			},
            appCode:"60092050",
            pageCode:"60092050nccloud",
            areaCode:"list",
        };

        try {
            let res = await dispatch({
                type: 'transfer/getWidgetTableData',
                payload: {
                    postData: postData
                }
            });
            let tableId = COMMON.tableId;
            if (res.success) {
                if (res.data && res.data.list) {
                    // 详情页翻页组件用
                    let billId = res.data.list.allpks || [];
                    cacheTools.set('allpks', billId);

                    let pageInfo = this.deepCopy(res.data.list.pageInfo);
                    dispatch({
                        type: 'transfer/update',
                        payload: {
                            pageInfo
                        }
                    });
                    editTable.setTableData(tableId, res.data[tableId], false);
                    editTable.hideColByKey(tableId, 'opr')
                }
                else {
                    cacheTools.set('allpks', []);
                    editTable.setTableData(tableId, {rows: []}, false);
                }
            }
        } catch (e) {

        }
    }
}
